package com.gold.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.gold.entity.AdminUser;
import com.gold.entity.GameType;
import com.gold.entity.Status;
import com.gold.myexception.Myexception;
import com.gold.service.GameTypeService;
import com.gold.service.StatusService;
import com.gold.util.Myutil;

@Controller
public class GameTypeController {

	@Autowired
	private GameTypeService gts;
	@Autowired
	private StatusService ss;

	@RequestMapping("getAllGameTypeByPage")
	public String getAllGameTypeByPage(String nowPageString, ModelMap mm, HttpServletRequest request) {
		AdminUser oldau = (AdminUser) request.getSession().getAttribute("adminLoginUser");
		if (oldau == null) {
			return "redirect:gold/index.jsp";
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("nowPageString", nowPageString);
		List<GameType> gameTypeList = gts.selectGameTypeByPage(map);
		List<Status> gameTypeStatusList = ss.selectAllGameStatus();
		mm.addAttribute("gameTypeList", gameTypeList);
		mm.addAttribute("gameTypeStatusList", gameTypeStatusList);
		mm.addAttribute("nowPage", map.get("nowPageString"));
		return "/gold/gameType/gametype.jsp";
	}

	@RequestMapping("getGameTypeByLike")
	public String getGameTypeByLike(String nowPageString, String typeName, String typeStatusId, ModelMap mm) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("nowPageString", nowPageString);
		map.put("typeName", typeName);
		map.put("typeStatusId", typeStatusId);
		List<GameType> gameTypeList = gts.selectGameTypeByLikeAndPage(map);
		List<Status> gameTypeStatusList = ss.selectAllGameStatus();
		mm.addAttribute("gameTypeList", gameTypeList);
		mm.addAttribute("gameTypeStatusList", gameTypeStatusList);
		mm.addAttribute("nowPage", map.get("nowPageString"));
		return "/gold/gameType/gametype.jsp";
	}

	@RequestMapping("insertGameType")
	public String insertGameType(@RequestParam("typePicture") CommonsMultipartFile file, String typeName, String typeStatusId, ModelMap mm) {
		System.out.println(Myutil.checkFileNull(file));
		if (Myutil.checkFileNull(file)) {
			if (Myutil.checkStringNull(typeName, typeStatusId)) {
				String fileName = file.getOriginalFilename();
				String newFileName = Myutil.getDateString() + fileName + fileName.substring(fileName.lastIndexOf("."));
				String path = "/home/soft01/upload/gametype/";
				try {
					file.transferTo(new File(path + newFileName));
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Map<String, String> map = new HashMap<String, String>();
				map.put("typePicture", newFileName);
				map.put("typeName", typeName);
				map.put("typeStatusId", typeStatusId);
				try {
					gts.insertGameType(map);
				} catch (Myexception e) {
					mm.addAttribute("msg", e.getMessage());
				}
			} else {
				mm.addAttribute("msg", "添加失败");
			}
		} else {
			mm.addAttribute("msg", "添加文件失败");
		}
		return "getAllGameTypeByPage.do";
	}

	@RequestMapping("updateGameType")
	public String updateGameType(String typeId, String update, String typeStatusId, ModelMap mm) {
		if (Myutil.checkStringNull(typeId, update)) {
			if (!"true".equals(update)) {
				GameType gameType = gts.selectGameTypeByTypeId(typeId);
				List<Status> gameTypeStatusList = ss.selectAllGameStatus();
				mm.addAttribute("gameTypeStatusList", gameTypeStatusList);
				mm.addAttribute("updateGameType", gameType);
				return "/gold/gameType/updateGameType.jsp";
			}
			if (Myutil.checkStringNull(typeStatusId)) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("typeStatusId", typeStatusId);
				map.put("typeId", typeId);
				gts.updateGameType(map);
			}
		}
		return "getAllGameTypeByPage.do";
	}

	@RequestMapping("deleteGameType")
	public String deleteGameType(String[] typeId, ModelMap mm) {
		if (typeId != null) {
			try {
				gts.deleteGameType(typeId);
			} catch (Myexception e) {
				mm.addAttribute("msg", e.getMessage());
			}
		} else {
			mm.addAttribute("msg", "删除失败");
		}
		return "getAllGameTypeByPage.do";
	}

}
